package com.qianfeng.microservice.consumer.controller;

//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//                  佛祖镇楼                  BUG辟易
//          佛曰:
//                  写字楼里写字间，写字间里程序员；
//                  程序人员写程序，又拿程序换酒钱。
//                  酒醒只在网上坐，酒醉还来网下眠；
//                  酒醉酒醒日复日，网上网下年复年。
//                  但愿老死电脑间，不愿鞠躬老板前；
//                  奔驰宝马贵者趣，公交自行程序员。
//                  别人笑我忒疯癫，我笑自己命太贱；
//


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qianfeng.microservice.consumer.pojo.Address;
import com.qianfeng.microservice.consumer.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Random;

/**
 * Created by Jackiechan on 2022/9/14 21:53
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@RestController
@RequestMapping("/users")
public class UserController {

    private RestTemplate restTemplate;


    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @GetMapping("/user/{id}")
    @HystrixCommand(fallbackMethod ="findByIdFallback",commandProperties ={@HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE")} )
    public User findById(@PathVariable Integer id) {
        System.err.println("findById"+Thread.currentThread().getName());
        User user = new User();
        user.setId(id);
        user.setAge(18 + id);
        user.setName("张三" + id);
        Address address = restTemplate.getForObject("http://m09provider-hystrix/address/info/" + (id + 1), Address.class);
        user.setAddress(address);
        return user;
    }


    public User findByIdFallback(Integer id) {
        System.err.println("findByIdFallback"+Thread.currentThread().getName());
        User user = new User();
        user.setId(-1000);
        user.setAge(20 + id);
        user.setName("find降级" + id);
        user.setAddress(new Address());
        return user;
    }




    @GetMapping("/user2/{id}")
    @HystrixCommand(fallbackMethod ="findById2Fallback" )
    public User findById2(@PathVariable Integer id) {
        System.err.println("findById2"+Thread.currentThread().getName());
        User user = new User();
        user.setId(id);
        user.setAge(18 + id);
        user.setName("张三" + id);
        Address address = restTemplate.getForObject("http://m09provider-hystrix/address/info/?id=" + (id + 1) + "&name=laowang", Address.class);
        user.setAddress(address);
        if (new Random().nextBoolean()) {
            //模拟一个异常
            int i = 1 / 0;
        }
        return user;
    }

    public User findById2Fallback( Integer id) {
        System.err.println("findById2Fallback"+Thread.currentThread().getName());
        User user = new User();
        user.setId(id);
        user.setAge( -100);
        user.setName("降级" + id);
        user.setAddress(new Address());
        return user;
    }



    @GetMapping("/test3")
    @HystrixCommand(fallbackMethod = "test3Fallback")
    public String test3() {
        System.err.println("UserController中的方法test3 执行了--->");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "test33333";
    }

    public String test3Fallback() {
        return "test3Fallback";
    }


    @GetMapping("/test4")
    @HystrixCommand(fallbackMethod = "test4Fallback")
    public String test4() {
        System.err.println("test4====>"+Thread.currentThread().getName());
        int i = 1 / 0;
        return "test4444444";
    }

    public String test4Fallback() {
        System.err.println("test4Fallback====>"+Thread.currentThread().getName());
        return "test4Fallback";
    }
}
